﻿package com.jfdesgagne.jfdesgagne09.core.service {
	import flash.net.*;
	import com.jfdesgagne.jfdesgagne09.core.service.RemotingService;
/***************************************************
	Class RemotingGateway AS3
----------------------------------------------------
	This file manage connection between AMFPHP and Flash.
	Than the result is send to LoadManager to distribute data.
----------------------------------------------------
	History of file
----------------------------------------------------
	2007-07-19 --> File created 
***************************************************/ 
public class RemotingGateway extends NetConnection {
	/*
	About: Description
	This file manage connection between AMFPHP and Flash.
	Than the result is send to <LoadManager> to distribute data.
	
	About: Author		
	Jean-François Desgagné jfdesgagne[-at-]gmail.com
	
	About: License		
	Creative Commons http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode
	
	About: File Created    
	July 19th 2007
	
	About: Last modifications	
	July 19th 2007
*/
/***************************************************
	Variables 
***************************************************/
	/* Constant: GATEWAY_URL 
	   Url of troisiemeOeil's gateway.*/
	public  static const GATEWAY_URL   : String = "system/application/php/amfphp/gateway.php";
	/* Variable: rgRemotingService 
	   Reference of <RemotingService> controller.*/
	private var    rgRemotingService   : RemotingService;
	/* Variable: rgRemotingResponder 
	   Responder object.*/
	private var    rgRemotingResponder : Responder;
	/* Variable: rgCalleeFunction 
	   Function to call when data is received.*/
	private var    rgCalleeFunction	   : Function;
/***************************************************
	Constructor 
***************************************************/
	/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Constructor: 	RemotingGateway
						Assign arguments to class properties
						
		Parameters:		
						_url 	  - Url of AMFPHP gateway.
						_encoding - AMF encodage string.
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
	public function RemotingGateway(_url:String = GATEWAY_URL , _encoding:Number = ObjectEncoding.AMF3){
		trace("_url:"+_url);
		this.rgRemotingService   = new RemotingService(_url,_encoding);
		this.rgRemotingResponder = new Responder(dataSucceed,dataError);
	}
/***************************************************
	Private Method 
***************************************************/
	/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Function: 		dataSucceed
						Action to do when data transfer succeed
						
		Parameters:		
						_data 	  - Object returned by AMFPHP.
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
	private function dataSucceed(_data:Object):void{
		trace("dataSucceed");
		this.rgCalleeFunction(_data);
	}
	/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Function: 		dataError
						Action to do when data transfer fail.
						
		Parameters:		
						_data 	  - Object returned by AMFPHP.
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
	private function dataError(_data:Object):void{
		trace("********************************************");
		throw new Error( _data.description    +"\n"+
						"Level: "+_data.level +"\n"+
						"Line : "+_data.line  +"\n"+
						"Code : "+_data.code  +"\n");
	}
/***************************************************
	Public Method 
***************************************************/
	/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Function: 		run
						Make one request to AMFPHP
						
		Parameters:		
						_service   - Service to call.
						_function  - Function that receive data
						_arguments - An array containing arguments for AMFPHP
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
	public function run(_service:String,_function:Function, _arguments:Array=null):void{
		trace("lalala:"+_arguments);
		var params:Object   	  = new Object();
		this.rgCalleeFunction = _function;
		/*
		if(_arguments!=null){
			for(var i:Number; i<_arguments.length;i++){
				params["arg"+i] = _arguments[i];
			}
		}
		*/
		this.rgRemotingService.call(_service,this.rgRemotingResponder,_arguments);
	}
}
}